/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.filter;

import com.blandware.atleap.webapp.util.core.StatisticsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>Filter which collects statistics.</p>
 * <p><a href="StatisticsCollectionFilter.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.5 $ $Date: 2008/01/30 08:57:23 $
 * @web.filter name="statisticsCollectionFilter"
 * @web.filter-init-param name="isEnabled" value="${statistics.enabled}"
 */
public final class StatisticsCollectionFilter implements Filter {

    /**
     * Log instance
     */
	protected transient final Log log = LogFactory.getLog(StatisticsCollectionFilter.class);

    /**
     * Contains config for the filter
     */
	protected FilterConfig config = null;

    /**
     * Whether statistics collection is enabled
     */
    protected boolean isEnabled = true;

    /**
     * Filters a request collecting statistics data.
     *
     * @param servletRequest    request we are serving
     * @param servletResponse   response we are creating
     * @param filterChain       filter chain
     * @throws IOException
     * @throws ServletException
     */
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain)
            throws IOException, ServletException {

        if (isEnabled) {
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            
            StatisticsUtil.serveVisit(request, response, false);
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

    /**
     * Initializes a filter.
     *
     * @param filterConfig filter config
     * @throws ServletException
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        this.config = filterConfig;
        isEnabled = Boolean.valueOf(filterConfig.getInitParameter("isEnabled")).booleanValue();
    }

    /**
     * Destroys a filter.
     */
    public void destroy() {
        this.config = null;
    }
}
